package longrise.phone.com.bjjt_henan.service;


import android.app.Application;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;


/**
 * Created by admin on 2016-11-18.
 */

public class LocationHelper {

    private LocationCallBack callBack;
    private static LocationHelper helper;

    private LocationClient locationClient;
    private BDLocationListener locationListener = new MyBDLocationListener();
    private LocationClientOption mOption;
    private Object objLock = new Object();

    private LocationHelper(Application application) {
        //第一步实例化定位核心类
        locationClient = new LocationClient(application, getLocOption());
        locationClient.registerLocationListener(locationListener);
    }

    public static LocationHelper getInstance(Application application) {
        if (helper == null) {
            helper = new LocationHelper(application);
        }
        return helper;
    }

    public void start() {
//        开始定位
        synchronized (objLock) {
            if (locationClient != null && !locationClient.isStarted()) {
                locationClient.start();
            }
        }
    }

    //一般会在Activity的OnDestroy方法调用
    public void stop() {
        synchronized (objLock) {
            if (locationClient != null && locationClient.isStarted()) {
                locationClient.stop();
            }
        }
    }

    public void destroy() {
        if (locationClient != null) {
            locationClient.unRegisterLocationListener(locationListener);
            locationClient.stop();
            locationClient = null;
        }
    }

    private LocationClientOption getLocOption() {

        if (null == mOption) {
            mOption = new LocationClientOption();
            mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
            mOption.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系，如果配合百度地图使用，建议设置为bd09ll;
            mOption.setScanSpan(0);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
            mOption.setIsNeedAddress(false);//可选，设置是否需要地址信息，默认不需要
            mOption.setIsNeedLocationDescribe(false);//可选，设置是否需要地址描述
            mOption.setOpenGps(true);
        }

        return mOption;
    }

    private class MyBDLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (callBack != null && bdLocation != null) {
                callBack.LocationcallBack(bdLocation.getLatitude() + "", bdLocation.getLongitude() + "");
            }
            //多次定位必须要调用stop方法
            if (null != locationClient) {
                locationClient.stop();
            }

        }
    }

    public interface LocationCallBack {
        void LocationcallBack(String Latitude, String Longitude);
    }

    public LocationCallBack getCallBack() {
        return callBack;
    }

    public void setLocationCallBack(LocationCallBack callBack) {
        this.callBack = callBack;
    }
}

